package com.bawei.caohao0802.view.activity

import android.content.Intent
import android.net.ConnectivityManager
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import coil.load
import coil.transform.RoundedCornersTransformation
import com.ayvytr.adapter.smart
import com.bawei.caohao0802.R
import com.bawei.caohao0802.databinding.ActivityLeiBinding
import com.bawei.caohao0802.model.bean.LeiBean
import com.bawei.caohao0802.viewmodel.MainVewiModel
import com.bawei.caohao20240730.base.BaseActivity
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader

class LeiActivity : BaseActivity<MainVewiModel,ActivityLeiBinding>(R.layout.activity_lei) {
    val leiadapter = smart(mutableListOf<LeiBean.Result>(), R.layout.lei_mo, { item, position ->
        if (item.liveStatus == 1) {
            findViewById<TextView>(R.id.type).text = "直播中"
        } else {
            findViewById<TextView>(R.id.type).text = "录播"
        }
        findViewById<TextView>(R.id.name).text = item.nickName
//        成网格条目网络图片展示
        findViewById<ImageView>(R.id.img).load(item.liveCoverUrl) {
//            4．网络图片加载中显示占位图，加载失败显示错误图
            error(R.mipmap.ic_launcher)
            placeholder(R.mipmap.ic_launcher)
            transformations(RoundedCornersTransformation(40f))
        }
    }) { }.click { item, position ->
//        8．点击图2的网格条目打开图3
        startActivity(Intent(this,TvActivity::class.java).putExtra("id",item.liveId).putExtra("type",item.liveStatus))
    }
    override fun dataObser() {
        mViewModel.leidata.observe(this, androidx.lifecycle.Observer {
            leiadapter.update(it.result,true)
        })
    }
    var page=1
    override fun initView() {
        var manager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
        var networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        if(networkInfo!=null&&networkInfo.isConnected){
            Toast.makeText(this, "网络已连接诶", Toast.LENGTH_SHORT).show()
        }else{
            Toast.makeText(this, "请连接网络 ", Toast.LENGTH_SHORT).show()
        }


        mDing.kai.setOnClickListener {
            startActivity(Intent(this,ZhiBoActivity::class.java))
        }
        mDing.smart.setRefreshHeader(ClassicsHeader(this))
        mDing.smart.setRefreshFooter(ClassicsFooter(this))
        mDing.smart.setOnRefreshListener {
            page =1
            leiadapter.clear()
            mViewModel.getlei(1548, 100, page, 21)
            mDing.smart.finishRefresh()

        }
        mDing.smart.setOnLoadMoreListener {
            page++
            mViewModel.getlei(1548, 100, page, 21)
            mDing.smart.finishLoadMore()
        }

//        参考图2完成首页网格展示
        mViewModel.getlei(1548, 100, page, 21)
        mDing.rec.adapter=leiadapter
        mDing.rec.layoutManager= GridLayoutManager(this,2)
    }



}